const RouterSystem = require("../template")

function testRouterSystemPerformance() {
  console.log(
    "1. 大量路由注册性能测试:",
    (() => {
      const router = new RouterSystem()
      const routeCount = 1000
      
      const start = performance.now()
      
      for (let i = 0; i < routeCount; i++) {
        const component = { name: `Component${i}` }
        router.register(`/route${i}`, component)
      }
      
      const end = performance.now()
      const registerTime = end - start
      
      return registerTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "2. 大量路由导航性能测试:",
    (() => {
      const router = new RouterSystem()
      const routeCount = 1000
      
      // 注册路由
      for (let i = 0; i < routeCount; i++) {
        const component = { name: `Component${i}` }
        router.register(`/route${i}`, component)
      }
      
      const start = performance.now()
      
      // 导航路由
      return new Promise((resolve) => {
        let completed = 0
        const total = 100
        
        for (let i = 0; i < total; i++) {
          router.navigate(`/route${i}`).then(() => {
            completed++
            if (completed === total) {
              const end = performance.now()
              const navigateTime = end - start
              resolve(navigateTime < 1000) // 1秒内完成
            }
          })
        }
      })
    })()
  )

  console.log(
    "3. 大量路由守卫性能测试:",
    (() => {
      const router = new RouterSystem()
      const guardCount = 100
      
      const start = performance.now()
      
      for (let i = 0; i < guardCount; i++) {
        const guard = (to, from, next) => next()
        router.addGuard(guard, 'before')
      }
      
      const end = performance.now()
      const guardTime = end - start
      
      return guardTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "4. 大量路由中间件性能测试:",
    (() => {
      const router = new RouterSystem()
      const middlewareCount = 100
      
      const start = performance.now()
      
      for (let i = 0; i < middlewareCount; i++) {
        const middleware = (to, from, next) => next()
        router.addMiddleware(middleware)
      }
      
      const end = performance.now()
      const middlewareTime = end - start
      
      return middlewareTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "5. 混合操作性能测试:",
    (() => {
      const router = new RouterSystem()
      const operationCount = 1000
      
      const start = performance.now()
      
      for (let i = 0; i < operationCount; i++) {
        const operation = Math.floor(Math.random() * 4)
        
        if (operation === 0) {
          const component = { name: `Component${i}` }
          router.register(`/route${i}`, component)
        } else if (operation === 1) {
          router.navigate(`/route${i}`)
        } else if (operation === 2) {
          const guard = (to, from, next) => next()
          router.addGuard(guard, 'before')
        } else {
          const middleware = (to, from, next) => next()
          router.addMiddleware(middleware)
        }
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "6. 路由系统内存效率测试:",
    (() => {
      const router = new RouterSystem()
      const routeCount = 1000
      
      // 添加大量路由
      for (let i = 0; i < routeCount; i++) {
        const component = { name: `Component${i}` }
        router.register(`/route${i}`, component)
      }
      
      // 验证内存使用合理
      const routes = router.getRoutes()
      
      return routes.length === routeCount
    })()
  )

  console.log(
    "7. 路由系统并发操作性能测试:",
    (() => {
      const router = new RouterSystem()
      const operationCount = 1000
      
      const start = performance.now()
      
      // 并发操作
      for (let i = 0; i < operationCount; i++) {
        const component = { name: `Component${i}` }
        router.register(`/route${i}`, component)
        router.navigate(`/route${i}`)
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000
    })()
  )

  console.log(
    "8. 路由系统批量操作性能测试:",
    (() => {
      const router = new RouterSystem()
      const batchSize = 100
      const batches = 10
      
      const start = performance.now()
      
      // 批量操作
      for (let batch = 0; batch < batches; batch++) {
        for (let i = 0; i < batchSize; i++) {
          const component = { name: `Batch${batch}_Component${i}` }
          router.register(`/batch${batch}_route${i}`, component)
        }
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000
    })()
  )

  console.log(
    "9. 路由系统历史操作性能测试:",
    (() => {
      const router = new RouterSystem()
      const routeCount = 100
      
      // 注册路由
      for (let i = 0; i < routeCount; i++) {
        const component = { name: `Component${i}` }
        router.register(`/route${i}`, component)
      }
      
      const start = performance.now()
      
      // 历史操作
      return new Promise((resolve) => {
        let completed = 0
        const total = 50
        
        for (let i = 0; i < total; i++) {
          router.navigate(`/route${i}`).then(() => {
            completed++
            if (completed === total) {
              router.back(25).then(() => {
                router.forward(25).then(() => {
                  const end = performance.now()
                  const historyTime = end - start
                  resolve(historyTime < 1000)
                })
              })
            }
          })
        }
      })
    })()
  )

  console.log(
    "10. 路由系统查询性能测试:",
    (() => {
      const router = new RouterSystem()
      const routeCount = 1000
      
      // 添加路由
      for (let i = 0; i < routeCount; i++) {
        const component = { name: `Component${i}` }
        router.register(`/route${i}`, component)
      }
      
      const start = performance.now()
      
      // 查询操作
      for (let i = 0; i < routeCount; i++) {
        router.hasRoute(`/route${i}`)
      }
      
      const end = performance.now()
      const queryTime = end - start
      
      return queryTime < 1000
    })()
  )

  console.log(
    "11. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(count) {
          const router = new RouterSystem()
          const start = performance.now()
          
          for (let i = 0; i < count; i++) {
            const component = { name: `Component${i}` }
            router.register(`/route${i}`, component)
          }
          
          const end = performance.now()
          const time = end - start
          
          return {
            time: time,
            result: count * this.multiplier
          }
        }
      }
      
      const result = perfObj.processPerformance(100)
      
      return result.result === 200 &&
             result.time < 100
    })()
  )

  console.log(
    "12. 路由系统性能一致性测试:",
    (() => {
      const router1 = new RouterSystem()
      const router2 = new RouterSystem()
      const routeCount = 100
      
      // 相同操作测试一致性
      const times1 = []
      const times2 = []
      
      for (let i = 0; i < 10; i++) {
        const start1 = performance.now()
        for (let j = 0; j < routeCount; j++) {
          const component = { name: `Component${j}` }
          router1.register(`/route${j}`, component)
        }
        const end1 = performance.now()
        times1.push(end1 - start1)
        
        const start2 = performance.now()
        for (let j = 0; j < routeCount; j++) {
          const component = { name: `Component${j}` }
          router2.register(`/route${j}`, component)
        }
        const end2 = performance.now()
        times2.push(end2 - start2)
      }
      
      // 验证性能一致性
      const avgTime1 = times1.reduce((a, b) => a + b, 0) / times1.length
      const avgTime2 = times2.reduce((a, b) => a + b, 0) / times2.length
      
      return avgTime1 < 100 &&
             avgTime2 < 100 &&
             Math.abs(avgTime1 - avgTime2) < 50
    })()
  )
}

testRouterSystemPerformance()
